﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace halcon联合.自定义控件
{
    public partial class IndicatorLight : UserControl
    {
        public IndicatorLight()
        {
            InitializeComponent();
            this.Size = new Size(100, 100); // 默认大小
            this.DoubleBuffered = true;    // 防止闪烁

            // 启动闪烁控制器，设置间隔为 300ms
            blinkTimer = new Timer
            {
                Interval = 300 // 设置闪烁间隔为 300ms
            };
            blinkTimer.Tick += BlinkTimer_Tick;
        }
        // 当前状态，用 volatile 确保线程间可见性
        private volatile int currentState = 3; // 初始状态：3 = 灰色稳定

        // 上次重绘的时间戳
        private long lastRedrawTime = 0;

        // 控制闪烁的定时器
        private Timer blinkTimer;
        private bool isBlinking = false;

        // 定义指示灯的状态
        public enum IndicatorState
        {
            RedBlinking = 1,
            GreenStable = 2,
            GrayStable = 3
        }

        // 状态属性，更新状态时自动触发重绘
        public IndicatorState State
        {
            get => (IndicatorState)currentState;
            set
            {
                if (currentState != (int)value)
                {
                    currentState = (int)value;

                    // 如果是红色闪烁，启用定时器
                    if (value == IndicatorState.RedBlinking)
                    {
                        if (!blinkTimer.Enabled) // 确保计时器未启动时才启动
                        {
                            isBlinking = true;
                            blinkTimer.Start();  // 启动计时器
                        }
                    }
                    else
                    {
                        isBlinking = false;
                        blinkTimer.Stop();    // 停止计时器
                    }

                    // 标记需要重绘
                    RequestRedraw();
                }
            }
        }

        // 定时器控制闪烁逻辑
        private void BlinkTimer_Tick(object sender, EventArgs e)
        {
            if (State == IndicatorState.RedBlinking)
            {
                // 每300ms闪烁一次
                isBlinking = !isBlinking;
                RequestRedraw();
            }
        }

        // 标记重绘请求
        private void RequestRedraw()
        {
            long now = Environment.TickCount;

            // 只有当状态变化时，才进行重绘
            if (State != IndicatorState.RedBlinking || now - lastRedrawTime > 50)
            {
                lastRedrawTime = now;

                // 在主线程中触发重绘
                if (this.InvokeRequired)
                {
                    this.BeginInvoke((Action)(() => this.Invalidate())); // 确保重绘在UI线程执行
                }
                else
                {
                    this.Invalidate();
                }
            }
        }

        // 重绘方法
        protected override void OnPaint(PaintEventArgs e)
        {
            try
            {
                base.OnPaint(e);
                Graphics g = e.Graphics;
                g.SmoothingMode = SmoothingMode.AntiAlias;

                float width = this.Width;
                float height = this.Height;

                // 绘制外圈渐变
                RectangleF outerRec = new RectangleF(6, 6, width - 2 * 6, height - 2 * 6);
                using (LinearGradientBrush outerBrush = new LinearGradientBrush(
                    outerRec, Color.FromArgb(100, 100, 100), Color.FromArgb(192, 192, 192), LinearGradientMode.Vertical))
                {
                    outerBrush.SetBlendTriangularShape(0.5f, 1);
                    g.FillEllipse(outerBrush, outerRec);
                }

                // 绘制内圈阴影
                RectangleF shadowRec = new RectangleF(16, 16, width - 32, height - 32);
                using (Brush shadowBrush = new SolidBrush(Color.FromArgb(80, Color.Black)))
                {
                    g.FillEllipse(shadowBrush, shadowRec);
                }
                RectangleF innerRec = new RectangleF(16, 16, width - 2 * 16, height - 2 * 16);
                // 绘制内圈颜色
                Color startColor, endColor;

                if (State == IndicatorState.RedBlinking && isBlinking)
                {
                    // 闪烁灭灯时变灰
                    startColor = Color.FromArgb(211, 211, 211);
                    endColor = Color.FromArgb(105, 105, 105);
                }
                else
                {
                    switch (State)
                    {
                        case IndicatorState.RedBlinking: // 红色稳定
                            startColor = Color.FromArgb(255, 100, 100);
                            endColor = Color.FromArgb(200, 0, 0);
                            break;
                        case IndicatorState.GreenStable: // 绿色稳定
                            startColor = Color.FromArgb(144, 238, 144);
                            endColor = Color.FromArgb(0, 128, 0);
                            break;
                        case IndicatorState.GrayStable: // 灰色稳定
                        default:
                            startColor = Color.FromArgb(211, 211, 211);
                            endColor = Color.FromArgb(105, 105, 105);
                            break;
                    }
                }

                using (LinearGradientBrush innerBrush = new LinearGradientBrush(
                    innerRec, startColor, endColor, LinearGradientMode.Vertical))
                {
                    innerBrush.SetBlendTriangularShape(0.5f, 1);
                    g.FillEllipse(innerBrush, innerRec);
                }
            }
            catch
            {

            }
        }
    }
}

